# -*- encoding=utf8 -*-
__author__ = "yangxianming"

from airtest.core.api import *
# ST.PROJECT_ROOT = "E:\AirtestWorkSpace\fuyuanwaiUItest"
# using(r"E:\AirtestWorkSpace\fuyuanwaiUItest\common.air")
# from common import *

auto_setup(__file__)

#拿到设备编号
import sys
args = sys.argv
devicePid = args[4].split("/")[3]
print("--------" + devicePid)
rootArr = args[2].split("\\")
scriptRoot = ""
for i in range(len(rootArr) - 1):
    scriptRoot = scriptRoot + rootArr[i] + "\\"
print("--------" + scriptRoot)

#对更目录下设变编号文件做注册登记操作 1代表在使用  0代表停止使用
with open(scriptRoot + devicePid, 'w') as f:
    f.write('1')
    
using(scriptRoot + "common.air")

from common import *
stop_app("com.alibaba.android.rimet")
start_app("com.alibaba.android.rimet")

try:

    sleep(1.0)
    openKoufu()
    sleep(1.0)
    #点管理
    touch(Template(r"tpl1684305590283.png", record_pos=(0.4, 0.815), resolution=(1080, 1920)))
    sleep(1.0)
    #企业商城
    touch(Template(r"tpl1684305628399.png", record_pos=(-0.001, -0.136), resolution=(1080, 1920)))
    sleep(1.0)
    #多次向上滚动
    for i in range(10):
        swipe((538, 1700), (538, 400))
        sleep(1.0)
        
    #点击最后一张coco代金券
    touch(Template(r"tpl1684305899552.png", record_pos=(-0.248, 0.583), resolution=(1080, 1920)))
    sleep(1.0)
    swipe((538, 1700), (538, 800))
    sleep(1.0)
    touch(Template(r"tpl1684306017608.png", record_pos=(-0.005, -0.024), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684306533533.png", record_pos=(0.33, 0.629), resolution=(1080, 1920)))
    sleep(1.0)
    keyevent("KEYCODE_DEL")
    sleep(1.0)
    text("25")
    sleep(1.0)
    touch(Template(r"tpl1684307378048.png", record_pos=(-0.005, 0.749), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307403286.png", record_pos=(0.275, 0.155), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307429151.png", record_pos=(0.303, 0.812), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307448985.png", record_pos=(-0.003, 0.807), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307481185.png", record_pos=(-0.003, 0.183), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307505910.png", record_pos=(0.343, -0.153), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307529976.png", record_pos=(0.312, -0.473), resolution=(1080, 1920)))
    sleep(1.0)
    text("自回归-发生日")
    sleep(1.0)
    touch(Template(r"tpl1684307749814.png", record_pos=(0.352, -0.331), resolution=(1080, 1920)))
    sleep(1.0)
    swipe((538, 1700), (538, 1200))
    sleep(1.0)
    touch(Template(r"tpl1684307899186.png", record_pos=(-0.003, 0.394), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307918983.png", record_pos=(0.414, 0.094), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684307943520.png", target_pos=5, record_pos=(-0.021, -0.029), resolution=(1080, 1920)))
    sleep(4.0)
    touch(Template(r"tpl1684308838654.png", record_pos=(-0.348, -0.272), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684308876330.png", record_pos=(0.282, 0.818), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684308931874.png", target_pos=6, record_pos=(-0.019, 0.109), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684308952697.png", record_pos=(0.416, 0.091), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684308973233.png", record_pos=(0.241, 0.8), resolution=(1080, 1920)))
    sleep(1.0)
    touch((200, 650), duration=0.1)
    sleep(1.0)
    touch(Template(r"tpl1684309567912.png", target_pos=6, record_pos=(-0.022, 0.012), resolution=(1080, 1920)))
    text("小明公司日常自动化巡视验证测试。请勿使用发放的卡券")
    sleep(1.0)
    touch(Template(r"tpl1684309780288.png", target_pos=6, record_pos=(-0.026, 0.206), resolution=(1080, 1920)))
    sleep(1.0)
    text("机器人小明")
    sleep(1.0)
    touch(Template(r"tpl1684309826584.png", record_pos=(-0.003, 0.749), resolution=(1080, 1920)))
    sleep(1.0)
    touch(Template(r"tpl1684309893053.png", record_pos=(-0.001, 0.799), resolution=(1080, 1920)))
    sleep(1.0)
    
    assert_exists(Template(r"tpl1684309927438.png", record_pos=(0.0, -0.365), resolution=(1080, 1920)), "发放成功就成功了")



    
except Exception as e:
    print(e)
finally:
    sleep(1.0)
    stop_app("com.alibaba.android.rimet")
    #完成测试等级设备为0以便被其他脚本使用
    with open(scriptRoot + devicePid, 'w') as f:
        f.write('0')
